package com.store.service;

import com.store.dao.ExpressrecordMapper;
import com.store.model.ExpressrecordModel;
import com.store.model.ResultData;
import com.store.utils.ConstantVariable;
import com.store.utils.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ArrayList;

@Service("expressrecordService")
public class ExpressrecordService {

    @Autowired
    private ExpressrecordMapper expressrecordMapper;

    @Autowired
    private ExcelUtil<ExpressrecordModel> excelUtil;

    public XSSFWorkbook export(ExpressrecordModel model) {
        model.setPageSize(Integer.MAX_VALUE);
        model.setPageNow(0);
        List<ExpressrecordModel> dataList = getList(model);
        return excelUtil.exportExcel(model, dataList);
    }

    public void saveRecord(String orderNo, String errorCode) {
        ExpressrecordModel expressrecordModel = new ExpressrecordModel();
        expressrecordModel.setOrderno(orderNo);
        expressrecordModel.setRecordtype(errorCode);
        insert(expressrecordModel);
    }

    public ExpressrecordModel validateRecord(String orderNo, String errorCode) {
        ExpressrecordModel expressrecordModel = new ExpressrecordModel();
        expressrecordModel.setOrderno(orderNo);
        expressrecordModel.setRecordtype(errorCode);
        return getModelById(expressrecordModel);
    }

    public ResultData getData(ExpressrecordModel model) {
        ResultData returnData = new ResultData();
        try {
            returnData.setDataList(this.getList(model));
            int dataCount = this.getCount(model);
            returnData.setPageCount(dataCount, model.getPageSize());
            returnData.setDataCount(dataCount);
            returnData.setState(ConstantVariable.SUCCESS);
            returnData.setPageNumber(model.getPageNumber());
        } catch (Exception e) {
            returnData.setState(ConstantVariable.ERROR);
            returnData.setMsg(e.getMessage());
        }
        return returnData;
    }

    public ExpressrecordModel getModelById(ExpressrecordModel model) {
        List<ExpressrecordModel> dataList = getList(model);
        if (dataList.size() > 0) {
            return dataList.get(0);
        }
        return null;
    }

    public List<ExpressrecordModel> getList(ExpressrecordModel model) {
        return expressrecordMapper.getList(model);
    }

    public int getCount(ExpressrecordModel model) {
        return expressrecordMapper.getCount(model);
    }

    public ExpressrecordModel insert(ExpressrecordModel model) {
        expressrecordMapper.insert(model);
        return model;
    }

    public ExpressrecordModel update(ExpressrecordModel model) {
        expressrecordMapper.update(model);
        return model;
    }

    public List<ExpressrecordModel> batchUpdate(List<ExpressrecordModel> modelList) {
        for (ExpressrecordModel model : modelList) {
            expressrecordMapper.update(model);
        }
        return modelList;
    }

    public void disableOrEnable(ExpressrecordModel model) {
        expressrecordMapper.disableOrEnable(model);
    }

    public void delete(ExpressrecordModel model) {
        List<String> idList = new ArrayList<>();
        idList.add(model.getRecordid());
        model.setIdList(idList);
        expressrecordMapper.delete(model);
    }

    public void deleteByField(ExpressrecordModel model) {
        expressrecordMapper.deleteByField(model);
    }

    public String batchDelete(List<String> idList) {
        ExpressrecordModel model = new ExpressrecordModel();
        model.setIdList(idList);
        expressrecordMapper.delete(model);
        return ResultData.toSuccessString();
    }

}